package com.project.easy.common.framework.generator.controller;

import com.project.easy.common.framework.generator.entity.FrameworkGeneratorProject;
import com.project.easy.common.framework.generator.entity.FrameworkGeneratorProjectPackage;
import com.project.easy.common.framework.generator.entity.dto.FrameworkGeneratorProjectExcuteDto;
import com.project.easy.common.framework.generator.entity.dto.GeneratorDto;
import com.project.easy.common.framework.generator.entity.dto.PageGeneratorDto;
import com.project.easy.common.framework.generator.service.IFrameworkGeneratorProjectService;
import com.project.easy.common.framework.web.annotation.ApiValidator;
import com.project.easy.common.framework.web.annotation.AuthValidator;
import com.project.easy.common.framework.web.controller.BaseController;
import com.project.easy.common.log.annotation.Logging;
import  com.project.easy.common.util.constants.SysModelConstant;
import  com.project.easy.common.util.constants.VersionConstant;
import  com.project.easy.common.util.dto.OneLongApiContextParam;
import  com.project.easy.common.util.dto.PageResDto;
import  com.project.easy.common.util.dto.R;
import com.project.easy.common.framework.generator.service.IFrameworkGeneratorProjectPackageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author GreatorMan.cool
 * @since 2021-09-28
 */
@RestController
@RequestMapping("/api/" + VersionConstant.ONE + "/generator/project")
public class FrameworkGeneratorProjectController extends BaseController {

    @Autowired
    private IFrameworkGeneratorProjectService frameworkGeneratorProjectService;

    @Autowired
    private IFrameworkGeneratorProjectPackageService iFrameworkGeneratorProjectPackageService;

    /**
     * 分页查询项目
     * @param param 入参
     * @return 返回分页数据
     */
    @PostMapping(value = "/pageList")
    @Logging(value = SysModelConstant.GENERATOR)
    @ApiValidator
    public R<PageResDto<FrameworkGeneratorProject>> pageList(@RequestBody PageGeneratorDto param) {
        return R.success(this.frameworkGeneratorProjectService.pageList(param));
    }
    /**
     * 新增
     * @param param 入参
     * @return 返回ID 新增成功ID
     */
    @PostMapping(value = "/add")
    @Logging(value = SysModelConstant.GENERATOR)
    @ApiValidator
    public  R<Long>  add(@RequestBody GeneratorDto param) {
        return R.success(this.frameworkGeneratorProjectService.add(param));
    }
    /**
     * 修改
     * @param param 入参
     * @return 返回True 成功 False 失败
     */
    @PostMapping(value = "/update")
    @Logging(value = SysModelConstant.GENERATOR)
    @ApiValidator
    public  R<Boolean>  udpate(@RequestBody GeneratorDto param) {
        return R.success(this.frameworkGeneratorProjectService.update(param));
    }
    /**
     * 启用
     * @param param ID
     * @return 返回 true 启用成功
     */
    @PostMapping(value = "/able")
    @Logging(value = SysModelConstant.GENERATOR)
    @ApiValidator
    public  R<Boolean>  able(@RequestBody OneLongApiContextParam param) {
        return R.success(this.switchStatus(frameworkGeneratorProjectService, v -> v::setStatus, Arrays.asList(param.getKey()), FrameworkGeneratorProject::getStatus));
    }
    /**
     * 禁用
     * @param param ID
     * @return 返回 true 启用成功
     */
    @PostMapping(value = "/disable")
    @Logging(value = SysModelConstant.GENERATOR)
    @ApiValidator
    public  R<Boolean>  disable(@RequestBody OneLongApiContextParam param) {
        return R.success(this.switchStatus(frameworkGeneratorProjectService, v -> v::setStatus, Arrays.asList(param.getKey()), FrameworkGeneratorProject::getStatus));
    }

    /**
     * 查询项目包
     * @param param ID
     * @return 返回 项目包数据
     */
    @PostMapping(value = "/queryProjectPackage")
    @Logging(value = SysModelConstant.GENERATOR)
    @ApiValidator
    public  R<List<FrameworkGeneratorProjectPackage>>  queryProjectPackage(@RequestBody OneLongApiContextParam param) {
        return R.success(this.iFrameworkGeneratorProjectPackageService.queryList(param.getKey()));
    }

    /**
     * 生成代码
     * @param param ID
     * @return 返回 true 启用成功
     */
    @PostMapping(value = "/excute")
    @Logging(value = SysModelConstant.GENERATOR)
    @AuthValidator(hasToken = false)
    public  R<Boolean>  excute(@RequestBody FrameworkGeneratorProjectExcuteDto param) {
        return R.success(this.frameworkGeneratorProjectService.excute(param));
    }
}
